#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Drawing;
using System.Windows.Forms;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Factories;
using WaveTech.GShield.Engine.Systems;
using WaveTech.GShield.Engine.Tools;
using WaveTech.GShield.Manager.Properties;
using WaveTech.GShield.Model;
using Exception=System.Exception;

#endregion

namespace WaveTech.GShield.Manager
{
	public partial class RatingPolicies : Form
	{
		#region Constructor

		public RatingPolicies()
		{
			InitializeComponent();
			InitializeRequiredSystems();
		}

		#endregion Constructor

		#region Initialize Required Systems

		private void InitializeRequiredSystems()
		{
		}

		#endregion Initialize Required Systems

		#region RatingPolicies_Load

		private void RatingPolicies_Load(object sender, EventArgs e)
		{
			m_bannerImage.Image = BannerFactory.CreateBanner(m_bannerImage.Width,
			                                                 m_bannerImage.Height, BannerFactory.BannerStyle.WinVistaBlack,
			                                                 new Bitmap(Resources.B256X256_RatingPolicy, new Size(48, 48)),
			                                                 "Ratings Policies",
			                                                 "ESRB Game Rating Policies");

			PopulateUsersLists();
		}

		#endregion RatingPolicies_Load

		#region Populate Users Lists

		private void PopulateUsersLists()
		{
			cboNewUserUsernames.Items.Clear();
			cboNewUserUsernames.Items.AddRange(Users.GetUsersList());

			lstRatingsUsers.DataSource = RatingsPolicyDAO.GetAllRatingPolicyUsers();
		}

		#endregion Populate Users Lists

		#region lstRatingsUsers_SelectedIndexChanged

		private void lstRatingsUsers_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstRatingsUsers.SelectedItem != null && lstRatingsUsers.SelectedItem.ToString().Trim().Length > 0)
			{
				Cursor.Current = Cursors.WaitCursor;

				RatingsPolicy p = RatingsPolicyDAO.GetRatingPolicyForUser(lstRatingsUsers.SelectedItem.ToString());

				if (p.Username != null)
					cboNewUserUsernames.Text = p.Username;

				if (p.AllowEC.HasValue)
					chkAllowEC.Checked = p.AllowEC.Value;

				if (p.AllowE.HasValue)
					chkAllowEveryone.Checked = p.AllowE.Value;

				if (p.Allow10P.HasValue)
					chkAllow10P.Checked = p.Allow10P.Value;

				if (p.AllowT.HasValue)
					chkAllowTeen.Checked = p.AllowT.Value;

				if (p.AllowM.HasValue)
					chkAllowMature.Checked = p.AllowM.Value;

				if (p.AllowAO.HasValue)
					chkAllowAdultsOnly.Checked = p.AllowAO.Value;

				if (p.AllowRP.HasValue)
					chkAllowRatingPending.Checked = p.AllowRP.Value;

				if (p.AllowUnknown.HasValue)
					chkAllowUnknown.Checked = p.AllowUnknown.Value;
			}
			else
			{
				cboNewUserUsernames.Text = "";
				chkAllowEC.Checked = false;
				chkAllowEveryone.Checked = false;
				chkAllow10P.Checked = false;
				chkAllowTeen.Checked = false;
				chkAllowMature.Checked = false;
				chkAllowAdultsOnly.Checked = false;
				chkAllowRatingPending.Checked = false;
				chkAllowUnknown.Checked = false;
			}

			Cursor.Current = Cursors.Default;
		}

		#endregion lstRatingsUsers_SelectedIndexChanged

		#region btnDeleteRatingUser_Click

		private void btnDeleteRatingUser_Click(object sender, EventArgs e)
		{
			if (lstRatingsUsers.SelectedItem != null && lstRatingsUsers.SelectedItem.ToString().Length > 0)
			{
				if (
					MessageBox.Show(
						"Are your sure you want to remove the rating policy for this user " + lstRatingsUsers.SelectedItem +
						" from GShield database?", "Remove Rating Policy", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
					DialogResult.Yes)
				{
					if (RatingsPolicyDAO.GetRatingPolicyForUser(lstRatingsUsers.SelectedItem.ToString()) != null)
					{
						RatingsPolicy p = RatingsPolicyDAO.GetRatingPolicyForUser(lstRatingsUsers.SelectedItem.ToString());

						if (p != null)
						{
							try
							{
								RatingsPolicyDAO.Delete(p);
							}
							catch (Exception ex)
							{
								MessageBox.Show("There there was an error trying to delete the rating policy, " + ex.Message,
								                "Delete Ratings User Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
							}

							Logging.Write("A ratings policy for the user " + lstRatingsUsers.SelectedItem + " has been deleted.",
							              LogCategory.Usage, LogPriority.Medium, LogType.RatingPolicyDel,
							              lstRatingsUsers.SelectedItem.ToString());

							lstRatingsUsers.SelectedIndex = -1;

							cboNewUserUsernames.Text = "";
							chkAllowEC.Checked = false;
							chkAllowEveryone.Checked = false;
							chkAllow10P.Checked = false;
							chkAllowTeen.Checked = false;
							chkAllowMature.Checked = false;
							chkAllowAdultsOnly.Checked = false;
							chkAllowRatingPending.Checked = false;
							chkAllowUnknown.Checked = false;

							PopulateUsersLists();

							MessageBox.Show("Delete Ratings Policy successful.", "Delete Ratings Policy", MessageBoxButtons.OK,
							                MessageBoxIcon.Information);
						}
						else
						{
							MessageBox.Show(
								"There is no GShield user with a username of " + lstRatingsUsers.SelectedItem + ", delete failed.",
								"Delete Ratings User Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}
					else
					{
						MessageBox.Show("There is no Rating Policy for the user " + lstRatingsUsers.SelectedItem + ", delete failed.",
						                "Delete Ratings User Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
			else
			{
				MessageBox.Show("You must select a user to remove the rating policy for.", "Delete Rating Policy",
				                MessageBoxButtons.OK,
				                MessageBoxIcon.Error);
			}
		}

		#endregion btnDeleteRatingUser_Click

		#region btnSave_Click

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (cboNewUserUsernames.SelectedItem != null && cboNewUserUsernames.SelectedItem.ToString().Trim().Length > 0)
			{
				if (RatingsPolicyDAO.GetRatingPolicyForUser(cboNewUserUsernames.SelectedItem.ToString()) != null)
				{
					if (
						MessageBox.Show(
							"Are your sure you want to update the rating policy for the user " + cboNewUserUsernames.SelectedItem + "?",
							"Update Rating Policy", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
					{
						Cursor.Current = Cursors.WaitCursor;

						RatingsPolicy p = RatingsPolicyDAO.GetRatingPolicyForUser(cboNewUserUsernames.SelectedItem.ToString());

						if (p != null)
						{
							p.AllowEC = chkAllowEC.Checked;
							p.AllowE = chkAllowEveryone.Checked;
							p.Allow10P = chkAllow10P.Checked;
							p.AllowT = chkAllowTeen.Checked;
							p.AllowM = chkAllowMature.Checked;
							p.AllowAO = chkAllowAdultsOnly.Checked;
							p.AllowRP = chkAllowRatingPending.Checked;
							p.AllowUnknown = chkAllowUnknown.Checked;

							RatingsPolicyDAO.Save(p);
						}

						Logging.Write("A ratings policy for the user " + lstRatingsUsers.SelectedItem + " has been updated.",
						              LogCategory.Usage, LogPriority.Medium, LogType.RatingPolicyUp,
						              lstRatingsUsers.SelectedItem.ToString());

						lstRatingsUsers.SelectedIndex = -1;

						cboNewUserUsernames.Text = "";
						chkAllowEC.Checked = false;
						chkAllowEveryone.Checked = false;
						chkAllow10P.Checked = false;
						chkAllowTeen.Checked = false;
						chkAllowMature.Checked = false;
						chkAllowAdultsOnly.Checked = false;
						chkAllowRatingPending.Checked = false;
						chkAllowUnknown.Checked = false;

						PopulateUsersLists();

						Cursor.Current = Cursors.Default;

						MessageBox.Show("The ratings policy has been updated.", "Update Ratings Policy", MessageBoxButtons.OK,
						                MessageBoxIcon.Information);
					}
				}
				else
				{
					if (
						MessageBox.Show(
							"Are your sure you want to add the rating policy for the user " + cboNewUserUsernames.SelectedItem + "?",
							"Update Usage Policy", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
					{
						Cursor.Current = Cursors.WaitCursor;

						RatingsPolicy p = new RatingsPolicy();

						p.Username = cboNewUserUsernames.SelectedItem.ToString();
						p.AllowEC = chkAllowEC.Checked;
						p.AllowE = chkAllowEveryone.Checked;
						p.Allow10P = chkAllow10P.Checked;
						p.AllowT = chkAllowTeen.Checked;
						p.AllowM = chkAllowMature.Checked;
						p.AllowAO = chkAllowAdultsOnly.Checked;
						p.AllowRP = chkAllowRatingPending.Checked;
						p.AllowUnknown = chkAllowUnknown.Checked;

						RatingsPolicyDAO.Save(p);

						Logging.Write("A new ratings policy for the user " + cboNewUserUsernames.SelectedItem + " has been created.",
						              LogCategory.Usage, LogPriority.Medium, LogType.RatingPolicyAdd,
						              cboNewUserUsernames.SelectedItem.ToString());

						PopulateUsersLists();

						Cursor.Current = Cursors.Default;

						MessageBox.Show("The rating policy has been created.", "Create Rating Policy", MessageBoxButtons.OK,
						                MessageBoxIcon.Information);
					}
				}
			}
			else
			{
				Cursor.Current = Cursors.Default;

				MessageBox.Show("You must select a user to add the rating for.", "Create Rating Policy", MessageBoxButtons.OK,
				                MessageBoxIcon.Error);
			}

			Cursor.Current = Cursors.Default;
		}

		#endregion btnSave_Click

		#region Rating Help Buttons

		private void pciAllowECHelp_Click(object sender, EventArgs e)
		{
			MessageBox.Show(this,
			                "Titles rated EC (Early Childhood) have content that may be suitable for ages 3 and older. Contains no material that parents would find inappropriate.");
		}

		private void picEveryoneRatingHelp_Click(object sender, EventArgs e)
		{
			MessageBox.Show(this,
			                "Titles rated E (Everyone) have content that may be suitable for ages 6 and older. Titles in this category may contain minimal cartoon, fantasy or mild violence and/or infrequent use of mild language.");
		}

		private void picAllowEveryone10PRatingHelp_Click(object sender, EventArgs e)
		{
			MessageBox.Show(this,
			                "Titles rated E10+ (Everyone 10 and older) have content that may be suitable for ages 10 and older. Titles in this category may contain more cartoon, fantasy or mild violence, mild language and/or minimal suggestive themes.");
		}

		private void picAllowTeenRatingHelp_Click(object sender, EventArgs e)
		{
			MessageBox.Show(this,
			                "Titles rated T (Teen) have content that may be suitable for ages 13 and older. Titles in this category may contain violence, suggestive themes, crude humor, minimal blood, simulated gambling, and/or infrequent use of strong language.");
		}

		private void picAllowMatureRatingHelp_Click(object sender, EventArgs e)
		{
			MessageBox.Show(this,
			                "Titles rated M (Mature) have content that may be suitable for persons ages 17 and older. Titles in this category may contain intense violence, blood and gore, sexual content and/or strong language.");
		}

		private void pciAllowAdultsOnlyRatingHelp_Click(object sender, EventArgs e)
		{
			MessageBox.Show(this,
			                "Titles rated AO (Adults Only) have content that should only be played by persons 18 years and older. Titles in this category may include prolonged scenes of intense violence and/or graphic sexual content and nudity.");
		}

		private void pciAllowRatingPendingRatingHelp_Click(object sender, EventArgs e)
		{
			MessageBox.Show(this,
			                "Titles listed as RP (Rating Pending) have been submitted to the ESRB and are awaiting final rating. (This symbol appears only in advertising prior to a game's release.)");
		}

		private void pciAllowUnknownRatingHelp_Click(object sender, EventArgs e)
		{
			MessageBox.Show(this,
			                "A title will get a GShield Only Rating of (Unknown) when GShield is unable to properly identify the game's rating through automated proceedures and needs to be set manually.");
		}

		#endregion Rating Help Buttons

		#region btnClose_Click

		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		#endregion btnClose_Click

		#region btnHelp_Click

		private void btnHelp_Click(object sender, EventArgs e)
		{
			Help.ShowHelp(this, Settings.Default.HelpFileName, HelpNavigator.TopicId,
			              Settings.Default.HelpTopics_RatingsPolicyForm);
		}

		#endregion btnHelp_Click
	}
}